package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.StorageGatherDto;
import com.guigu.erp.mapper.StorageGatherMapper;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.StorageGatherDetailsService;
import com.guigu.erp.service.StorageGatherService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lenovo
 */
@Service
public class StorageGatherServiceImpl extends ServiceImpl<StorageGatherMapper, StorageGather>
        implements StorageGatherService {

    @Autowired
    private StorageGatherMapper storageGatherMapper;

    @Autowired
    private StorageGatherService storageGatherService;

    @Autowired
    private StorageGatherDetailsService storageGatherDetailsService;

    /**
     * 获取Id最大的入库单编号
     * @return
     */
    @Override
    public String getMaxGatherId() {
        StorageGather storageGather=storageGatherMapper.queryMaxGatherId();
        if (storageGather==null) {
            return null;
        }
        //获取入库单编号
        String gatherId=storageGather.getGatherId();
        return gatherId;
    }

    /**
     * 完成入库制定
     * @param storageGatherDto
     */
    @Override
    public void makeStorageGather(StorageGatherDto storageGatherDto) {
        //1.插入入库单
        this.save(storageGatherDto);

        //插入入库明细
        List<StorageGatherDetails> details=storageGatherDto.getDetails();
        for (int i=0;i<details.size();i++){
            StorageGatherDetails detail=details.get(i);
            //设置每条明细id为空
            detail.setId(null);
            //获取入库单的id
            Integer parentId=storageGatherDto.getId();
            //设置父级序号
            detail.setParentId(parentId);
            //设置入库标志 K002-1 已登记
            detail.setGatherTag("K002-1");
            //插入物料明细
            storageGatherDetailsService.save(detail);
        }
    }

    /**
     * 按条件分页查询入库单未复核
     * @param vo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<StorageGather> recheckPage(DesignFileVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<StorageGather> queryWrapper = new QueryWrapper<>();

        // 额外添加1个条件
        // 审核状态：审核未通过
        queryWrapper.eq("check_tag","S001-0");

        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<StorageGather> storageGatherList =this.list(queryWrapper);
        PageInfo<StorageGather> storageGatherPageInfo = new PageInfo<>(storageGatherList);

        //将pageinfo 转换成 pageutils
        PageUtils<StorageGather> pageUtils = new PageUtils<>();
        pageUtils.setRows(storageGatherPageInfo.getList());
        pageUtils.setTotal(storageGatherPageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<StorageGather> recheckPage2(DesignFileVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<StorageGather> queryWrapper = new QueryWrapper<>();

        // 额外添加1个条件
        // 审核状态：审核通过
        queryWrapper.eq("check_tag","S001-1");
        // 库存状态：已登记
        queryWrapper.eq("store_tag","K002-1");

        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<StorageGather> storageGatherList =this.list(queryWrapper);
        PageInfo<StorageGather> storageGatherPageInfo = new PageInfo<>(storageGatherList);

        //将pageinfo 转换成 pageutils
        PageUtils<StorageGather> pageUtils = new PageUtils<>();
        pageUtils.setRows(storageGatherPageInfo.getList());
        pageUtils.setTotal(storageGatherPageInfo.getTotal());

        return pageUtils;
    }

    /**
     * 带条件分页查询入库单
     * @param vo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<StorageGather> GatherListPage(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<StorageGather> wrapper = new QueryWrapper<StorageGather>();
        //审核状态：审核通过
        wrapper.eq("check_Tag", "S001-1");
        if(vo != null) {
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<StorageGather> storageGathers = this.list(wrapper);
        PageInfo<StorageGather> pageInfo = new PageInfo<StorageGather>(storageGathers);
        //PageInfo转PageUtils
        PageUtils<StorageGather> pageUtils = new PageUtils<StorageGather>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    /**
     * 带条件分页查询制定入库单
     * @param vo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<StorageGather> GatherListPage2(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<StorageGather> wrapper = new QueryWrapper<StorageGather>();
        //审核状态：审核通过
        wrapper.eq("store_Tag", "K002-1");
        if(vo != null) {
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<StorageGather> storageGathers = this.list(wrapper);
        PageInfo<StorageGather> pageInfo = new PageInfo<StorageGather>(storageGathers);
        //PageInfo转PageUtils
        PageUtils<StorageGather> pageUtils = new PageUtils<StorageGather>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    @Override
    public StorageGather listGatherById(String parentId) {
        StorageGather storageGather=storageGatherMapper.listGatherById(parentId);
        return storageGather;
    }
}
